package com.yeyouluo.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;

public class HbaseClientDemo {
    public static final String TABLE_NAME = "friend";

    Configuration conf = null;
    Connection conn = null;
    HBaseAdmin admin = null;

    @Before
    public void init() throws IOException {
        // 获取配置
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "linux121,linux122");
        conf.set("hbase.zookeeper.property.clientPort", "2181");

        // 获取hbase连接
        conn = ConnectionFactory.createConnection(conf);
    }

    /**
     * 创建表
     *
     * @throws IOException
     */
    @Test
    public void createTable() throws IOException {
        admin = (HBaseAdmin) conn.getAdmin();
        // 创建表描述器
        HTableDescriptor teacher = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
        // 设置列族描述器
        teacher.addFamily(new HColumnDescriptor("info"));
        // 执行创建操作
        admin.createTable(teacher);
        System.out.println("teacher表创建成功");
    }

    /**
     * 删除表
     *
     * @throws IOException
     */
    @Test
    public void deleteTable() throws IOException {
        admin = (HBaseAdmin) conn.getAdmin();
        // 执行创建操作
        admin.disableTables(TABLE_NAME);
        admin.deleteTable(TABLE_NAME);
        System.out.println("表[" + TABLE_NAME + "]删除成功");
    }

    /**
     * 插入一条数据
     *
     * @throws IOException
     */
    @Test
    public void putData() throws IOException {
        // 获取table对象
        Table table = conn.getTable(TableName.valueOf(TABLE_NAME));

        // 准备put对象
        Put put = new Put(Bytes.toBytes("110"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("addr"), Bytes.toBytes("beijing"));

        // 插入数据
        table.put(put);
        System.out.println("插入一条数据成功");
    }

    /**
     * 删除一条数据
     *
     * @throws IOException
     */
    @Test
    public void deleteData() throws IOException {
        // 获取table对象
        Table table = conn.getTable(TableName.valueOf(TABLE_NAME));

        // 准备delete对象
        Delete delete = new Delete(Bytes.toBytes("uid1"));
        delete.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid2"));

//        Collection<List<Cell>> values = delete.getFamilyCellMap().values();
//        for (List<Cell> value : values) {
//            for (Cell cell : value) {
////                String rawRow = Bytes.toString((CellUtil.cloneRow(cell)));
////                String rawCF = Bytes.toString((CellUtil.cloneFamily(cell)));
////                String rawCQ = Bytes.toString((CellUtil.cloneQualifier(cell)));
//                System.out.println("rowkey=" + Bytes.toString(CellUtil.cloneRow(cell)) +
//                        "\tcf=" + Bytes.toString(CellUtil.cloneFamily(cell)) +
//                        "\tcq=" + Bytes.toString(CellUtil.cloneQualifier(cell)) +
//                        "\tval=" + Bytes.toString(CellUtil.cloneValue(cell))
//                );
//            }
//        }

        // 删除数据
        table.delete(delete);
        System.out.println("删除一条数据成功");
    }

    /**
     * 查询列族数据
     *
     * @throws IOException
     */
    @Test
    public void getDataByCF() throws IOException {
        // 获取table对象
        Table table = conn.getTable(TableName.valueOf(TABLE_NAME));


        // 准备get对象
        Get get = new Get(Bytes.toBytes("110"));
        get.addFamily(Bytes.toBytes("info"));

        // 查询数据
        Result result = table.get(get);

        Cell[] cells = result.rawCells();
        Arrays.stream(cells).forEach(x ->
                System.out.println("rowkey=" + Bytes.toString(CellUtil.cloneRow(x)) +
                        "\tcf=" + Bytes.toString(CellUtil.cloneFamily(x)) +
                        "\tcq=" + Bytes.toString(CellUtil.cloneQualifier(x)) +
                        "\tval=" + Bytes.toString(CellUtil.cloneValue(x))
                ));
    }

    /**
     * 扫描数据
     *
     * @throws IOException
     */
    @Test
    public void scanData() throws IOException {
        // 获取table对象
        Table table = conn.getTable(TableName.valueOf(TABLE_NAME));

        // 准备scan对象
        Scan scan = new Scan();
//        scan.setStartRow(Bytes.toBytes("001"));
//        scan.setStopRow(Bytes.toBytes("004"));
//        scan.setStopRow(Bytes.toBytes("2"));

        // 查询数据
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            Cell[] cells = result.rawCells();
            Arrays.stream(cells).forEach(x ->
                    System.out.println("rowkey=" + Bytes.toString(CellUtil.cloneRow(x)) +
                            "\tcf=" + Bytes.toString(CellUtil.cloneFamily(x)) +
                            "\tcq=" + Bytes.toString(CellUtil.cloneQualifier(x)) +
                            "\tval=" + Bytes.toString(CellUtil.cloneValue(x))
                    ));
        }
    }

    @After
    public void destroy() {
        if (admin != null) {
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
